# -*- coding: utf-8 -*-
# @File  : deal_excel.py
# @Author: David
# @Date  : 2022/1/1
# @Desc  : 处理Excel表格数据，填充没有完成的数据。

import wx

import filedialog
from m_sqlite3 import sqlite, sql_search
from openpyxl_excel import update_excel, update_excel_history


class TestFrame(wx.Frame):
    def __init__(self, parent):
        self.threads = []
        wx.Frame.__init__(self, parent, -1, "处理Excel表格", size=(640, 640))
        self.init_ui()

    def init_ui(self):
        self.SetSizeHints(wx.DefaultSize, wx.DefaultSize)

        self.m_menu_bar = wx.MenuBar(0)
        self.m_menu = wx.Menu()
        self.m_menu_item = wx.MenuItem(self.m_menu, wx.ID_ANY, u"打开xlsx表格", wx.EmptyString, wx.ITEM_NORMAL)
        self.m_menu_item_history = wx.MenuItem(self.m_menu, wx.ID_ANY, u"打开xlsx表格--更新历史数据", wx.EmptyString,
                                               wx.ITEM_NORMAL)
        self.m_menu_item_sql = wx.MenuItem(self.m_menu, wx.ID_ANY, u"同步到SQL", wx.EmptyString,
                                           wx.ITEM_NORMAL)
        self.m_menu.Append(self.m_menu_item)
        self.m_menu.Append(self.m_menu_item_history)
        self.m_menu.Append(self.m_menu_item_sql)
        self.m_menu_bar.Append(self.m_menu, u"文件")
        self.SetMenuBar(self.m_menu_bar)

        bSizer1 = wx.BoxSizer(wx.HORIZONTAL)
        self.m_textCtrl1 = wx.TextCtrl(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0)
        bSizer1.Add(self.m_textCtrl1, 0, wx.ALL, 5)
        self.m_button1 = wx.Button(self, wx.ID_ANY, u"MyButton", wx.DefaultPosition, wx.DefaultSize, 0)
        bSizer1.Add(self.m_button1, 0, wx.ALL, 5)
        self.SetSizer(bSizer1)
        self.Layout()

        self.Centre(wx.BOTH)

        # Connect Events
        self.Bind(wx.EVT_MENU, self.on_new, id=self.m_menu_item.GetId())
        self.Bind(wx.EVT_MENU, self.deal_excel_history, id=self.m_menu_item_history.GetId())
        self.Bind(wx.EVT_MENU, self.deal_sql, id=self.m_menu_item_sql.GetId())

        self.m_button1.Bind(wx.EVT_BUTTON, self.chose)

    def on_new(self, e):
        """处理涨停表格"""
        m_data = update_excel(filedialog.file_dialog(self, e))
        print(m_data)

    def deal_excel_history(self, e):
        """处理历史涨停表格"""
        files = filedialog.file_dialog(self, e)
        for file in files:
            m_data = update_excel_history(file)
            print(m_data)

    def deal_sql(self, e):
        """处理历史涨停表格"""
        files = filedialog.file_dialog(self, e)
        for file in files:
            m_data = sqlite(file)
            print(m_data)

    # Virtual event handlers, overide them in your derived class
    def chose(self, event):
        event.Skip()
        text = self.m_textCtrl1.GetLineText(0)
        print(text)
        # sql_search("select * from sqlite_master where '概念'='医药'")
        sql_search("SELECT * FROM '2021-12-28'")


if __name__ == '__main__':
    app = wx.App(False)
    frame = TestFrame(None)
    frame.Show(True)
    # import wx.lib.inspection
    # wx.lib.inspection.InspectionTool().Show()
    app.MainLoop()
